/**
 * 文件名称 : AndroidMusic.java
 * <p>
 * 作者信息 : maidou
 * <p>
 * 创建时间 : 2011-8-3, 上午7:33:05
 * <p>
 * 版权声明 : Copyright (c) 2009-2012 CIeNET Ltd. All rights reserved
 * <p>
 * 评审记录 :
 * <p>
 */

package com.ttomato.game.framework.impl;

import android.content.res.AssetFileDescriptor;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;

import com.ttomato.game.framework.Music;

/**
 * 请在这里增加文件描述
 * <p>
 */
public class AndroidMusic implements Music, OnCompletionListener
{
    
    MediaPlayer mMediaPlayer;
    
    boolean isPrepared = false;
    
    public AndroidMusic(AssetFileDescriptor assetFileDescriptor)
    {
        mMediaPlayer = new MediaPlayer();
        
        try
        {
            mMediaPlayer.setDataSource(assetFileDescriptor.getFileDescriptor(),
                    assetFileDescriptor.getStartOffset(),
                    assetFileDescriptor.getLength());
            mMediaPlayer.prepare();
            
            isPrepared = true;
            
            mMediaPlayer.setOnCompletionListener(this);
        }
        catch (Exception e)
        {
            // TODO Auto-generated catch block
            throw new RuntimeException("Couldn't load music");
        }
    }
    
    
    public void onCompletion(MediaPlayer mp)
    {
        // TODO Auto-generated method stub
        synchronized (this)
        {
            isPrepared = false;
        }
    }
    
    
    public void play()
    {
        // TODO Auto-generated method stub
        if (mMediaPlayer.isPlaying())
        {
            return;
        }
        
        try
        {
            synchronized (this)
            {
                if (!isPrepared)
                {
                    mMediaPlayer.prepare();
                }
                mMediaPlayer.start();
                
            }
        }
        catch (Exception e)
        {
            // TODO: handle exception
            e.printStackTrace();
        }
        
    }
    
    
    public void stop()
    {
        // TODO Auto-generated method stub
        mMediaPlayer.stop();
        synchronized (this)
        {
            isPrepared = false;
        }
    }
    
    
    public void pause()
    {
        // TODO Auto-generated method stub
        
    }
    
    
    public void setLoogping(boolean looping)
    {
        // TODO Auto-generated method stub
        mMediaPlayer.setLooping(looping);
    }
    
    
    public void setVolume(float volume)
    {
        // TODO Auto-generated method stub
        mMediaPlayer.setVolume(volume, volume);
    }
    
    
    public boolean isPlaying()
    {
        // TODO Auto-generated method stub
        return mMediaPlayer.isPlaying();
    }
    
    
    public boolean isStopped()
    {
        // TODO Auto-generated method stub
        return !isPrepared;
    }
    
    
    public boolean isLooping()
    {
        // TODO Auto-generated method stub
        return mMediaPlayer.isLooping();
    }
    
    
    public void dispose()
    {
        // TODO Auto-generated method stub
        if (mMediaPlayer.isPlaying())
        {
            mMediaPlayer.stop();
        }
        mMediaPlayer.release();
    }
    
}
